<?php

namespace App\Kuafu\Shop\Validate;

use App\Exceptions\CustomValidateException;
use App\Http\Validate\AbstractBaseValidate;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\WaiMai\Model\WaiMaiShopMappingModel;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ShopNewValidate extends AbstractBaseValidate
{

    /**
     * @inheritDoc
     */
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [
            "id"            => [
                "required",
                "int",
                Rule::exists("shops", "id")
            ],
            "shopId"        => [
                "required",
                "int",
                Rule::exists("shops", "id")
            ],
            "shopCode"      => [
                "required",
                "int",
                Rule::exists("shops", "shopCode")
            ],
            "name"          => self::stringValidateBuild(true, 32),
            "address"       => self::stringValidateBuild(true, 255),
            "addressPrefix" => self::stringValidateBuild(true, 120),
            "province"      => self::stringValidateBuild(true, 32),
            "city"          => self::stringValidateBuild(true, 32),
            "district"      => self::stringValidateBuild(true, 32),
            "lng"           => self::stringValidateBuild(true, 36),
            "lat"           => self::stringValidateBuild(true, 36),
        ];
    }

    /**
     * @inheritDoc
     */
    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    /**
     * @inheritDoc
     */
    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
        Validator::extend('mobile', function ($attribute, $value, $parameters, $validator) {
            return $validator->validateRegex(
                $attribute,
                $value,
                ['/^1\d{10}$/'],
                "手机号格式非法"
            );
        });
    }

    /**
     * @inheritDoc
     */
    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }


    /**
     * 选址报告完成同步门店地址信息
     *
     * @param array $params
     * @return MessageBag|object
     * @throws CustomValidateException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function synchronizeShopInfoValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "id",
                "address",
                "addressPrefix",
                "province",
                "city",
                "district",
                "lng",
                "lat"
            ],
            $params
        );
    }


    /**
     * 门店启用
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function switchShopOnValidate(array $params)
    {
        return self::getInstance()->validate(["shopId"], $params);
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function closeShopValidate(array $params)
    {
        return self::getInstance()->validate(
            ["shopCode"],
            $params
        );
    }

    /**
     * 更改手机号校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function updateShopMobileValidate(array $params)
    {
        return self::getInstance()->validate(
            ["shopCode", "mobile", "contact"],
            $params,
            [
                "mobile"  => "required|string|mobile",
                "contact" => self::stringValidateBuild(true, 12, 1)
            ]
        );
    }

    /**
     * 城市门店列表
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function cityShopValidate(array $params)
    {
        return self::getInstance()->validate(
            ["cityCode", 'name'],
            $params,
            [
                "cityCode" => "required|int|min:1",
                "name"     => "string",
            ]
        );
    }

    /**
     * 城市门店列表
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function likeSearchValidate(array $params)
    {
        return self::getInstance()->validate(
            ["searchWhere", "withFields"],
            $params,
            [
                "searchWhere" => "required|string|min:1",
                "withFields"  => "required|boolean"
            ]
        );
    }


    /**
     * 门店列表
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function likeSearchParamsValidate(array $params)
    {
        return self::getInstance()->validate(
            ["searchWhere", "withFields"],
            $params,
            [
                "searchWhere" => "required|string|min:1",
                "withFields"  => "required"
            ]
        );
    }

    /**
     * 获取门店详情验证
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function getShopDetailByShopIdValidate(array $params)
    {
        return self::getInstance()->validate(
            ["shopId"],
            $params,
            [
                "shopId" => [
                    "required",
                    "int",
                    Rule::exists((new Shop())->getTable(), "id")
                ],
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function searchXhsShopListValidate(array $params)
    {
        return self::getInstance()->validate(
            ["search"],
            $params,
            [
                "search" => "required|string"
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function bindXhsShopValidate(array $params)
    {
        return self::getInstance()->validate(
            ["poiId", "shopId"],
            $params,
            [
                "shopId" => [
                    "required",
                    "int",
                    Rule::exists((new Shop())->getTable(), "id")
                ],
                "poiId"  => [
                    "required",
                    "string"
                ]
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function unbindXhsShopValidate(array $params)
    {
        return self::getInstance()->validate(
            ["shopId"],
            $params,
            [
                "shopId" => [
                    "required",
                    "int",
                    Rule::exists((new Shop())->getTable(), "id")
                ]
            ]
        );
    }

    /**
     * 抖音券码核销开关验证
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function dyCouponVerifySwitchValidate(array $params)
    {
        return self::getInstance()->validate(
            ["shopId", "switch"],
            $params,
            [
                "shopId" => [
                    "required",
                    "int",
                    Rule::exists((new Shop())->getTable(), "id")
                ],
                "switch" => self::intInValidateBuild(true, Shop::DY_COUPON_VERIFY_SWITCH_TEXT)
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function shopSelectValidate(array $params)
    {
        return self::getInstance()->validate(
            ["isCity", "status", "openFyStatus", 'isDistrict'],
            $params,
            [
                "isCity"       => "sometimes|in:0,1",
                "status"       => "sometimes|required|in:0,1,2",
                "openFyStatus" => "sometimes|required|in:0,1",
                'isDistrict'   => 'sometimes|in:0,1',
            ]
        );
    }
}
